<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sitedisapproved_model extends My_Model
{    
    public function __construct()
    {
        parent::__construct();
        $this->model_name="错误记录";
        $this->tablename="sites_disapproved";  
    }
        
    public function getpage($siteid,$page,$pagesize=0,$key="",$sort="",$code){
    
        if($siteid>0)
            $where=["dream_sites_disapproved.siteid"=>$siteid];
        else
            $where=[];
        if($pagesize>0)
        {
            $this->pagesize=$pagesize;
        }
        if($key)
        {
            $key=["title"=>$key];
        }
        if($code)
        {
            $where["code"]=$code;
        }
        if($sort)
        {
            $sort=$sort." desc";
        }
        else
        {
            $sort="sellcount desc,id desc";
        }
        $join=array(
            "table"=>"sites_product",
            "where"=>"dream_sites_product.productid = dream_sites_disapproved.prid and dream_sites_product.siteid = dream_sites_disapproved.siteid ",
            "pattern"=>"left"
        );
        if($siteid>0)
        {
            $baseurl ="backend/sites/disapproved/index/".$siteid;
        }
        else
        {
            $baseurl ="backend/shoppingerror";
        }

        return $this->get_items_by_page($where,$page,$baseurl,$sort,$join,$key,"dream_sites_disapproved.*,dream_sites_product.sellcount");                
    }

    public function getitemsfilter($filter)
    {
        return $this->get_items_by_where($filter);
    }   
    
    public function truncate()
    {
        $this->db->truncate($this->table_prefix.$this->tablename);
    }
   
    public function getdicount($siteid)
    {
        $where=["siteid"=>$siteid];
        $count=$this->getcount($where);
        return $count;
    }

    public function getall(){
        $where=[];
        return $this->get_items_by_where($where);                
    }

    public function getitemkeyfilter($filter)
    {
        return $this->get_item_by_where($filter);
    }


    public function getcodes($siteid=0)
    {
        
        if($siteid>0)
        {
            $where=["siteid"=>$siteid];
        }
        else
        {
            $where = [];
        }
        $query=$this->db->select('code')->where($where)->distinct('code')->get($this->table_prefix.$this->tablename);
        $item=$query->result_array();
        if($item){
            return $item;
        }else{
            return false;
        }
    }

    public function delitem($id)
    {
        $where=["id"=>$id];
        $this->delete($where);
        return true;
    }

    public function getitembyid($id){
        $where=array("id"=>$id);
  
        return $this->get_item_by_where($where);
    }

    public function create($data){
        $check =  $this->get_item_by_where(["skuid"=>$data["skuid"],"siteid"=>$data["siteid"]]);
        if(empty($check))
        {
            if(!isset($data["addtime"])){
                $data["addtime"]=date("Y-m-d H:i:s");
            }
           
            return $this->add_item($data);      
        }
        return false;
    }

    public function updatelink($info,$siteid)
    {
        $newdata=["url"=>$info["url"]];
        $where="siteid=".$siteid." and rid=".$info["id"];
        return $this->update_item($where,$newdata);
    }


    public function update($id,$data){

        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
           
        }else{
            $where=array("id"=>$id);
           
        }
        return $this->update_item($where,$data);

    }

   
}
